package com.cl.esjsoup.controller;

import com.cl.esjsoup.pojo.Comment;
import com.cl.esjsoup.service.CommentService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class CommentController {
    final CommentService commentService;

    public CommentController(CommentService commentService) {
        this.commentService = commentService;
    }

    @GetMapping("/parse/{start}/{end}")
    public Map<Integer, Boolean> parse(@PathVariable Integer start, @PathVariable Integer end) {
        Map<Integer, Boolean> map = new HashMap<>();
        try {
            for (int i = start; i <= end; i++) {
                boolean b = commentService.parseComment(i);
                map.put(i, b);
            }
            return map;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }
    @GetMapping("/list/{keyWord}/{page}/{size}")
    public List<Comment> list(@PathVariable Integer page, @PathVariable Integer size, @PathVariable String keyWord) {
        try {
            return commentService.list(keyWord, page, size);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Collections.emptyList();
    }
}
